package com.crm.approval.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crm.model.FinanceApply;
import com.crm.model.SysUser;
import com.crm.service.FinanceApplyService;
import com.crm.util.ResponseUtil;
import com.crm.util.SessionModel;
import com.crm.util.SessionUtils;
import com.github.pagehelper.Page;

@Controller
@RequestMapping("/apply")
public class FinanceApplyController {
	
	@Autowired
	private FinanceApplyService financeApplyService;
	
	@RequestMapping("/index")
	public String index(HttpServletRequest request){
		SysUser sysUser= ((SysUser)SessionUtils.get(SessionModel.SESSION_USER));
		request.setAttribute("user", sysUser);
		return "approval/apply";
	}
	
	
	@RequestMapping("/getAll")
	@ResponseBody
	public Map<String, Object> getAll(Page<FinanceApply> page, FinanceApply t){
		Map<String, Object> map = new HashMap<>();
		Page<FinanceApply> list = (Page<FinanceApply>) financeApplyService.selectAll(page, t);
		
		map.put("list", list);
		map.put("status", "success");
		map.put("msg", "新增成功！");
		
		return map;
	}
	
	@RequestMapping("/addNewRecord")
	@ResponseBody
	public Map<String, Object> addNewRecord(FinanceApply record){
		
		Map<String, Object> map = new HashMap<>();
		
		financeApplyService.insert(record);
		
		map.put("status", "success");
		map.put("msg", "新增成功！");
		
		return map;
	}
	
	@RequestMapping("/updateRecord")
	@ResponseBody
	public Map<String, Object> updateRecord(FinanceApply record){
		
		Map<String, Object> map = new HashMap<>();
		
		financeApplyService.updateByPrimaryKey(record);
		
		map.put("status", "success");
		map.put("msg", "修改成功！");
		
		return map;
	}
	
	@RequestMapping("/selectAllByWhere")
	@ResponseBody
	public void selectAllByWhere(HttpServletResponse response,HttpServletRequest request,Page<FinanceApply> page,FinanceApply t){
		Page<FinanceApply> list = (Page<FinanceApply>) financeApplyService.selectAllByWhere(page, t);
		ResponseUtil.resultPage(response,request,list);
	}
	@RequestMapping("/getTotalMoneyByWhere")
	@ResponseBody
	public Map<String,Object> getTotalMoneyByWhere(FinanceApply t){
		Map<String,Object> retMap = new HashMap<>();
		int totalMoney = financeApplyService.getTotalMoneyByWhere(t);
		retMap.put("data", totalMoney);
		return retMap;
	}
}
